package com.taky.firstset;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.SeekBar.OnSeekBarChangeListener;

import com.taky.Property.PropertyManager;
import com.taky.bodymanager.R;
import com.taky.tabmanager.TabManagerFrame;

public class FirstSet_AimWeght extends Activity{

	Button startmain;
	Button back;
	
	Intent i;
	
	EditText aimWeight;
	
	TextView firstset_aimweight_basersc;
	
	float BMI;
	float Fat_Rate;
	float Nomal_Weight;
	
	float Height_cnt;
	float Weight_cnt;
	
	float weightchange;
	
	SeekBar BmiSeekBar;
	SeekBar nomalWeightSeekBar;
	SeekBar fatRateSeekBar;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.firstset_aimweight);
		
		startmain = (Button) findViewById(R.id.firstset_aimweight_next);
		back = (Button) findViewById(R.id.firstset_aimweight_prev);
		aimWeight = (EditText) findViewById(R.id.firstset_aimweight_aimweight);
		
		BmiSeekBar = (SeekBar)findViewById(R.id.firstset_aimweight_BMIseekbar);
        BmiSeekBar.setProgressDrawable(getResources().getDrawable(R.drawable.bmicustomseekbar));
        
        nomalWeightSeekBar = (SeekBar) findViewById(R.id.firstset_aimweight_nomalweight);
        nomalWeightSeekBar.setProgressDrawable(getResources().getDrawable(R.drawable.nomalweightcustomseekbar));
        
        fatRateSeekBar = (SeekBar) findViewById(R.id.firstset_aimweight_fatrate);
        fatRateSeekBar.setProgressDrawable(getResources().getDrawable(R.drawable.fatratecustomseekbar));
        
        firstset_aimweight_basersc = (TextView) findViewById(R.id.firstset_aimweight_basersc);
        
        
        BmiSeekBar.setEnabled(false);
        fatRateSeekBar.setEnabled(false);
        
        Height_cnt = Float.parseFloat(PropertyManager.getInstance().getUserHeight());
		Weight_cnt = Float.parseFloat(PropertyManager.getInstance().getUserWeight());
        
        
//		               비만도													BMI	
//			90% 이하 --------------------체중미달 -------------------18.5이하
//			90% ~ 109% -----------------표준체중 -------------------18.5 ~ 22.9
//			110% ~ 119% ----------------과 체중 ---------------------23.0 ~ 24.9
//			120% ~ 149% ----------------비만 -----------------------25.0 ~ 29.9
//			150% 이상 -------------------고도비만 -------------------30.0이상
		
		
		
				//BMI지수 = 체중÷(신장×신장)｝×10000
				if(Height_cnt == 0){
					Height_cnt =+ (float) 0.1;
				}
				BMI = (Weight_cnt/(Height_cnt*Height_cnt))*10000; 
				
				
				
//                여성일경우 처음 세팅을 미용체중으로 계산한다
//				
//		                                                             미용체중 표
//		        		140cm  =>  37.24kg  ( 37kg )
//		        		141cm  =>  37.77kg  ( 38kg )
//		        		142cm  =>  38.31kg  ( 38kg )
//		        		143cm  =>  38.85kg  ( 39kg )
//		        		144cm  =>  39.40kg  ( 39kg )
//		        		145cm  =>  39.95kg  ( 40kg )
//		        		146cm  =>  40.50kg  ( 41kg )
//		        		147cm  =>  41.06kg  ( 41kg )
//		        		148cm  =>  41.62kg  ( 42kg )
//		        		149cm  =>  42.18kg  ( 42kg )
//		        		150cm  =>  42.75kg  ( 43kg )
//		        		151cm  =>  43.32kg  ( 43kg )
//		        		152cm  =>  43.90kg  ( 44kg )
//		        		153cm  =>  44.48kg  ( 44kg )
//		        		154cm  =>  45.06kg  ( 45kg )
//		        		155cm  =>  45.65kg  ( 46kg )
//		        		156cm  =>  46.24kg  ( 46kg )
//		        		157cm  =>  46.83kg  ( 47kg )
//		        		158cm  =>  47.43kg  ( 47kg )
//		        		159cm  =>  48.03kg  ( 48kg )
//		        		160cm  =>  48.64kg  ( 49kg )
//		        		161cm  =>  49.25kg  ( 49kg )
//		        		162cm  =>  49.86kg  ( 50kg )
//		        		163cm  =>  50.48kg  ( 50kg )
//		        		164cm  =>  51.10kg  ( 51kg )
//		        		165cm  =>  51.73kg  ( 52kg )
//		        		166cm  =>  52.36kg  ( 52kg )
//		        		167cm  =>  52.99kg  ( 53kg )
//		        		168cm  =>  53.63kg  ( 54kg )
//		        		169cm  =>  54.27kg  ( 54kg )
//		        		170cm  =>  54.91kg  ( 55kg )
//		        		171cm  =>  55.56kg  ( 56kg )
//		        		172cm  =>  56.21kg  ( 56kg )
//		        		173cm  =>  56.87kg  ( 57kg )
//		        		174cm  =>  57.52kg  ( 58kg )
//		        		175cm  =>  58.19kg  ( 58kg )
//		        		176cm  =>  58.85kg  ( 59kg )
//		        		177cm  =>  59.53kg  ( 60kg )
//		        		178cm  =>  60.20kg  ( 60kg )
//		        		179cm  =>  60.88kg  ( 61kg )
//		        		180cm  =>  61.56kg  ( 62kg )
				
				
				
				if(PropertyManager.getInstance().getUserSex().equals("woman")){
			        if(Height_cnt<140){
			        	//표준체중 = (신장 - 100)*0.9
			    		Nomal_Weight = (float) ((Height_cnt-100)*0.9);
			        }
			        else if(Height_cnt>140 && Height_cnt<=141){
			        	Nomal_Weight = 37.24f;
			        }
					else if(Height_cnt>141 && Height_cnt<=142){
					           Nomal_Weight = 37.77f;
					          }
					else if(Height_cnt>142 && Height_cnt<=143){
					           Nomal_Weight = 38.31f;
					          }
					else if(Height_cnt>143 && Height_cnt<=144){
					           Nomal_Weight = 38.85f;
					          }
					else if(Height_cnt>144 && Height_cnt<=145){
					           Nomal_Weight = 39.40f;
					          }
					else if(Height_cnt>145 && Height_cnt<=146){
					           Nomal_Weight = 39.95f;
					          }
					else if(Height_cnt>146 && Height_cnt<=147){
					           Nomal_Weight = 40.50f;
					          }
					else if(Height_cnt>147 && Height_cnt<=148){
					           Nomal_Weight = 41.06f;
					          }
					else if(Height_cnt>148 && Height_cnt<=149){
					           Nomal_Weight = 41.62f;
					          }
					else if(Height_cnt>149 && Height_cnt<=150){
					           Nomal_Weight = 42.18f;
					          }
					else if(Height_cnt>150 && Height_cnt<=151){
					           Nomal_Weight = 42.75f;
					          }
					else if(Height_cnt>151 && Height_cnt<=152){
					           Nomal_Weight = 43.32f;
					          }
					else if(Height_cnt>152 && Height_cnt<=153){
					           Nomal_Weight = 43.90f;
					          }
					else if(Height_cnt>153 && Height_cnt<=154){
					           Nomal_Weight = 44.48f;
					          }
					else if(Height_cnt>154 && Height_cnt<=155){
					           Nomal_Weight = 45.06f;
					          }
					else if(Height_cnt>155 && Height_cnt<=156){
					           Nomal_Weight = 45.65f;
					          }
					else if(Height_cnt>156 && Height_cnt<=157){
					           Nomal_Weight = 46.24f;
					          }
					else if(Height_cnt>157 && Height_cnt<=158){
					           Nomal_Weight = 46.83f;
					          }
					else if(Height_cnt>158 && Height_cnt<=159){
					           Nomal_Weight = 47.43f;
					          }
					else if(Height_cnt>159 && Height_cnt<=160){
					           Nomal_Weight = 48.03f;
					          }
					else if(Height_cnt>160 && Height_cnt<=161){
					           Nomal_Weight = 48.64f;
					          }
					else if(Height_cnt>161 && Height_cnt<=162){
					           Nomal_Weight = 49.25f;
					          }
					else if(Height_cnt>162 && Height_cnt<=163){
					           Nomal_Weight = 49.86f;
					          }
					else if(Height_cnt>163 && Height_cnt<=164){
					           Nomal_Weight = 50.48f;
					          }
					else if(Height_cnt>164 && Height_cnt<=165){
					           Nomal_Weight = 51.10f;
					          }
					else if(Height_cnt>165 && Height_cnt<=166){
					           Nomal_Weight = 51.73f;
					          }
					else if(Height_cnt>166 && Height_cnt<=167){
					           Nomal_Weight = 52.36f;
					          }
					else if(Height_cnt>167 && Height_cnt<=168){
					           Nomal_Weight = 52.99f;
					          }
					else if(Height_cnt>168 && Height_cnt<=169){
					           Nomal_Weight = 53.63f;
					          }
					else if(Height_cnt>169 && Height_cnt<=170){
					           Nomal_Weight = 54.27f;
					          }
					else if(Height_cnt>170 && Height_cnt<=171){
					           Nomal_Weight = 54.91f;
					          }
					else if(Height_cnt>171 && Height_cnt<=172){
					           Nomal_Weight = 55.56f;
					          }
					else if(Height_cnt>172 && Height_cnt<=173){
					           Nomal_Weight = 56.21f;
					          }
					else if(Height_cnt>173 && Height_cnt<=174){
					           Nomal_Weight = 56.87f;
					          }
					else if(Height_cnt>174 && Height_cnt<=175){
					           Nomal_Weight = 57.52f;
					          }
					else if(Height_cnt>175 && Height_cnt<=176){
					           Nomal_Weight = 58.19f;
					          }
					else if(Height_cnt>176 && Height_cnt<=177){
					           Nomal_Weight = 58.85f;
					          }
					else if(Height_cnt>177 && Height_cnt<=178){
					           Nomal_Weight = 59.53f;
					          }
					else if(Height_cnt>178 && Height_cnt<=179){
					           Nomal_Weight = 60.20f;
					          }
					else if(Height_cnt>179 && Height_cnt<=180){
					           Nomal_Weight = 60.88f;
					          }
					else if(Height_cnt>180 && Height_cnt<=181){
					           Nomal_Weight = 61.56f;
					          }
					else{
						//표준체중 = (신장 - 100)*0.9
						Nomal_Weight = (float) ((Height_cnt-100)*0.9);
					}
		        }
				else{
					//표준체중 = (신장 - 100)*0.9
					Nomal_Weight = (float) ((Height_cnt-100)*0.9);
				}
				
				//비만도 = ｛체중÷(신장-100)×0.9｝×100
			
				if(Nomal_Weight ==0){
					Nomal_Weight =+ (float) 0.1;
				}
				Fat_Rate = (float) (Weight_cnt/Nomal_Weight)*100;
        
	
				
				if(Fat_Rate < 70){
					Fat_Rate = 70f;
				}
				else if(Fat_Rate > 170){
					Fat_Rate = 170f;
				}
				
				
				if(BMI<15){
					BMI = 15f;
				}else if(BMI > 35){
					Fat_Rate = 35f;
				}
				
				if(Weight_cnt/Nomal_Weight<0){
					Weight_cnt = 0f;
				}else if(Weight_cnt/Nomal_Weight > 2){
					Weight_cnt = Nomal_Weight*2;
				}
				
				
				fatRateSeekBar.setProgress((int) (Fat_Rate-70));
		        BmiSeekBar.setProgress((int) ((BMI-15)*5));
		        
		       
		        
		        nomalWeightSeekBar.setProgress((int) (50*Weight_cnt/Nomal_Weight));
				
		        nomalWeightSeekBar.setOnSeekBarChangeListener(nomalWeightHandler);
		        
		        
        aimWeight.setText(String.format("%.2f", Nomal_Weight));
        
        
//        해리스 베네딕트의 기초대사량(BMR) 계산식은 다음과 같습니다.
//
//        ◦여자: 655 + (9.6 X 체중, kg) + (1.8 X 신장, cm) – (4.7 X 나이)
//        ◦남자: 66 + (13.7 X 체중, kg) + (5 X 신장, cm) – (6.8 X 나이)
        float k;
        if(PropertyManager.getInstance().getUserSex().equals("woman")){
        	k = (float) (655+(9.6*Weight_cnt)+(1.8*Height_cnt)-(4.7*Integer.parseInt(PropertyManager.getInstance().getUserAge())));
        }
        else{
        	k = (float) (66+(13.7*Weight_cnt)+(5*Height_cnt)-(6.8*Integer.parseInt(PropertyManager.getInstance().getUserAge())));
        }
        
        
        firstset_aimweight_basersc.setText(String.format("%.2f", k));
        
        
        OnClickListener bHandler = new OnClickListener() {		//클릭 이벤트 객체
    		public void onClick(View v) {
    			switch (v.getId()) {
    			
    			case R.id.firstset_aimweight_next:
    				
    				//먼저 값을 다 입력하였는지 확인을 한다!
    				PropertyManager.getInstance().setUserAimWeight(aimWeight.getText().toString());
    				i = new Intent(FirstSet_AimWeght.this, FirstSet_BodyType.class);
    				startActivity(i);
                    finish();   				
    				break;
    				
    			case R.id.firstset_aimweight_prev:
    				i = new Intent(FirstSet_AimWeght.this, FirstSet_SetWeight.class);
    				startActivity(i);
    				finish();
    				break;
    			}
    			
    			
    		}
    	};
    	startmain.setOnClickListener(bHandler);
    	back.setOnClickListener(bHandler);
    	
		
    
    	
	}

	public OnSeekBarChangeListener nomalWeightHandler = new OnSeekBarChangeListener() {
		
		@Override
		public void onStopTrackingTouch(SeekBar seekBar) {
			// TODO Auto-generated method stub
		}
		
		@Override
		public void onStartTrackingTouch(SeekBar seekBar) {
			// TODO Auto-generated method stub
		}
		
		@Override
		public void onProgressChanged(SeekBar seekBar, int progress,
				boolean fromUser) {
			// TODO Auto-generated method stub
			weightchange =  ((progress*Nomal_Weight)/50);
			
			if(Height_cnt == 0){
				Height_cnt =+ (float) 0.1;
			}
			BMI = (weightchange/(Height_cnt*Height_cnt))*10000; 
			
			
			//표준체중 = (신장 - 100)*0.9
			Nomal_Weight = (float) ((Height_cnt-100)*0.9);
			
			//비만도 = ｛체중÷(신장-100)×0.9｝×100
		
			if(Nomal_Weight ==0){
				Nomal_Weight =+ (float) 0.1;
			}
			Fat_Rate = (float) (weightchange/Nomal_Weight)*100;
    

			
			if(Fat_Rate < 70){
				Fat_Rate = 70f;
			}
			else if(Fat_Rate > 170){
				Fat_Rate = 170f;
			}
			
			
			if(BMI<15){
				BMI = 15f;
			}else if(BMI > 35){
				Fat_Rate = 35f;
			}
			
			if(weightchange/Nomal_Weight<0){
				weightchange = 0;
			}else if(weightchange/Nomal_Weight > 2){
				weightchange = (int) (Nomal_Weight*2);
			}
			
			
			fatRateSeekBar.setProgress((int) (Fat_Rate-70));
	        BmiSeekBar.setProgress((int) ((BMI-15)*5));
	        aimWeight.setText(String.format("%.2f", weightchange));
			
			
			
		}
	};
	
}
